package pactice_handler

import (
	"math/rand"
	"net/http"
	"peilian-api/app/global/databases/tables"
	"peilian-api/app/global/variable"
	"peilian-api/app/services"
	"peilian-api/utils/format"
	"peilian-api/utils/tools"
	"strconv"

	"github.com/gin-gonic/gin"
)

func GetOnTrailTimes(ctx *gin.Context) {
	count := services.GetOnTrailTimes(ctx)
	format.NewResponseJson(ctx).SuccessObj(struct {
		TrailCount int64 `json:"trail_count"`
	}{TrailCount: count})
}

func PracticeCreate(ctx *gin.Context) {
	var sq services.SessionReq
	if err := ctx.ShouldBindJSON(&sq); err != nil {
		format.NewResponseJson(ctx).SuccessObjMsg(30001, err.Error())
		return
	}
	id, err := services.PracticeCreate(ctx, sq.CareerId)
	if err != nil {
		format.NewResponseJson(ctx).Err(401, err.Error()) // 重新登录，返回错误码
		return
	}
	if err != nil && id == -1 {
		format.NewResponseJson(ctx).SuccessObjMsg(5001, err.Error())
		return
	}
	var u services.SessionResp
	u.Id = uint(id)
	ctx.JSON(200, gin.H{
		"code": 200,
		"data": u,
		"msg":  "OK",
	})
}

// 上传图片
func UploadPhoto(ctx *gin.Context) {
	strsid := ctx.Param("sessionId")
	sid, _ := strconv.Atoi(strsid)
	// 上传图片
	err := services.UpdatePhoto(ctx, uint(sid))
	if err != nil {
		format.NewResponseJson(ctx).Err(err.Code, err.Err.Error())
		return
	}
	out, err := services.GetMerginVideo(tables.Tpolar)
	if err != nil {
		format.NewResponseJson(ctx).Err(err.Code, err.Err.Error())
		return
	}
	format.NewResponseJson(ctx).SuccessDataList(len(out), out)
}

func SessionStart(ctx *gin.Context) {
	sessionId := ctx.Param("sessionId")
	sessionIdInt, err := strconv.Atoi(sessionId)
	if err != nil {
		format.NewResponseJson(ctx).Err(400, err.Error())
		return
	}
	initType := tables.Tinit
	session, err := services.FindSessionById(sessionIdInt)
	if err != nil {
		format.NewResponseJson(ctx).Err(400, err.Error())
		return
	}
	if session.CareerID == tables.QCivilServant {
		initType = tables.Tcivil
	}
	out, err1 := services.GetMerginVideo(initType)
	if err != nil {
		format.NewResponseJson(ctx).Err(err1.Code, err1.Err.Error())
		return
	}
	if len(out) != 0 {
		format.NewResponseJson(ctx).SuccessObj(out[0])
		return
	}
	format.NewResponseJson(ctx).SuccessObj(struct{}{})
}

// 更多练习功能

func CareerRecommend(c *gin.Context) {
	sessionIdStr := c.Param("sessionId")
	sessionId, err := strconv.Atoi(sessionIdStr)
	if err != nil {
		format.NewResponseJson(c).Err(400, err.Error())
		return
	}
	session, err := services.FindSessionById(sessionId)
	if err != nil {
		format.NewResponseJson(c).Err(400, err.Error())
		return
	}
	recommendCareer := make([]*tables.Career, 0)
	if session.Career.Type == "通用类" {
		allType := make([]string, 0)
		if err := variable.DB.MySQL.Model(&tables.Career{}).Where("type in (?)", []string{"互联网类", "传统类"}).
			Distinct("type").Find(&allType).Error; err != nil {
			format.NewResponseJson(c).Err(400, err.Error())
			return
		}
		for _, cType := range allType {
			theTypeCareers := make([]*tables.Career, 0)
			if err := variable.DB.MySQL.Where("type = ? and is_available = 0", cType).
				Find(&theTypeCareers).Error; err != nil {
				format.NewResponseJson(c).Error(400, err.Error())
				return
			}
			randSub := rand.Intn(len(theTypeCareers))
			recommendCareer = append(recommendCareer, theTypeCareers[randSub])
		}
	} else {
		recommendCareerLi := make([]*tables.Career, 0)
		if err := variable.DB.MySQL.Model(&tables.Career{}).
			Where("type = ? and id <> ? and is_available = 0", session.Career.Type, session.CareerID).
			Find(&recommendCareerLi).Error; err != nil {
			format.NewResponseJson(c).Err(400, err.Error())
			return
		}
		subCareer := tools.GenerateRandomNumber(0, len(recommendCareerLi), 2)
		for _, v := range subCareer {
			recommendCareer = append(recommendCareer, recommendCareerLi[v])
		}
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": recommendCareer[:2],
		"msg":  "OK",
	})
}
